package leetcode;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Ghost on 2014/10/31 0031.
 */
public class Restore_IP_Addresses {
    public List<String> list = new LinkedList<String>();
    public List<String> rst = new LinkedList<String>();
    public List<String> restoreIpAddresses(String s) {
        if(s.length()>12)
            return rst;
        getSep(s,1);
        return rst;
    }
    public void getSep(String s , int level){
        if(level<4 && s.length()!=0){
            for(int i=0;i<s.length();i++){
                String numStr = s.substring(0,i+1);
                int num = Integer.parseInt(numStr);
                if((num<256 && !numStr.startsWith("0")) || numStr.equals("0")){/////   "0" 单一的0
                    list.add(numStr);
                    getSep(s.substring(i+1),level+1);
                    list.remove(list.size()-1);
                }
                else if(num>255 || (numStr.length()>1 && numStr.startsWith("0")))
                    break;
            }
        }
        else if(level == 4){
            if(s.length()==0 || (s.length()>1 && s.startsWith("0")) || Integer.parseInt(s)>255){

            }
            else{
                String r = "";
                for(String ss:list){
                    r+=ss+".";
                }
                r+=s;
                rst.add(r);
            }
        }
    }
    public static void main(String[] args){
        Restore_IP_Addresses restore_ip_addresses = new Restore_IP_Addresses();
        System.out.println(restore_ip_addresses.restoreIpAddresses("0279245587303"));
    }
}
